<?php
	//echo Yii::app()->request->getParam("departemen","dep kosong");
	
	$departments = MDepartment::model()->findAll("is_active = TRUE order by id");
	$department_id = $department_id == "" ? $departments[0]->id : $department_id; 	
	$department_id = Yii::app()->request->getParam("department_id",$department_id);	
?>

<script>
function printData(){
window.print();
}
 
	 
	function newData(pelajaran_ke,data_hari){
//	alert($("#department_id option:selected").text());
//	return;
		var	karyawan_name=$("#karyawan_name").val();		
		var	tahun_ajaran_name=$("#tahun_ajaran_id option:selected").text();	
		var	tahun_ajaran_id=$("#tahun_ajaran_id").val();	
		var url="index.php?r=akademik/guru_pelajaran/jadwal_pelajaran/form&op=add&department_id=" + $("#department_id").val()+"&karyawan_id=" + $("#karyawan_id").val()+"&karyawan_name=" + karyawan_name+"&tahun_ajaran_name=" + tahun_ajaran_name+"&tahun_ajaran_id=" + tahun_ajaran_id+"&pelajaran_ke=" + pelajaran_ke+"&hari=" + data_hari;
		var winName = "Form Process";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=450,height=300;
		var replace = false,onCenter = true;
		var w = openWindow(winName,url,width,height,onCenter,specs,replace);
		w.focus();
		var timer = setInterval(function() {   
			if(w.closed) {  
				clearInterval(timer);  
				reloadPage();
			}  
		}, 1000);
	}
	/*
	function closeTest(){
		
	}
	*/
	function editRow(id,pelajaran_ke,pelajaran_sampai,data_hari,pelajaran_id,status){		
		var	karyawan_name=$("#karyawan_name").val();		
		var	tahun_ajaran_name=$("#tahun_ajaran_id option:selected").text();	
		var	tahun_ajaran_id=$("#tahun_ajaran_id").val();	
		var url="index.php?r=akademik/guru_pelajaran/jadwal_pelajaran/form&op=edit&id="+id+"&department_id=" + $("#department_id").val()+"&karyawan_id=" + $("#karyawan_id").val()+"&karyawan_name=" + karyawan_name+"&tahun_ajaran_name=" + tahun_ajaran_name+"&tahun_ajaran_id=" + tahun_ajaran_id+"&pelajaran_ke=" + pelajaran_ke+"&pelajaran_sampai=" + pelajaran_sampai+"&hari=" + data_hari+"&pelajaran_id=" + pelajaran_id+"&status=" + status;
		var winName = "Form Process";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=450,height=300;
		var replace = true,onCenter = true;
		var w = openWindow(winName,url,width,height,onCenter,specs,replace);
		w.focus();
		var timer = setInterval(function() {   
			if(w.closed) {  
				clearInterval(timer);  
				reloadPage();
			}  
		}, 1000);
	}
 
	function deleteRow(id){
		if (confirm("Delete data ?")){			
			window.parent.showLoader();			
			editUrl="index.php?r=akademik/guru_pelajaran/jadwal_pelajaran/edit"
			//alert(editUrl);	
			$.ajax({	
				url : editUrl,
				type:"GET",
				async:true,
				data: {op: "del",id:id},
				success: function(data) {
					if(data.actionStatus == 1){
						alert(data.actionMessage);
					}						
				},
				complete:function(){
					window.parent.hideLoader();
					location.reload();		
				}
			});			
		}
	}
  
</script>
 
<script>
	$(function(){
		$("#department_id").change(function(){
			var karyawan_id=0;
			var	department_id=$("#department_id").val();
			var	karyawan_name='';				
			var	tahunAjaran=$("#tahun_ajaran_id").val();								
			window.location.replace("index.php?r=akademik/guru_pelajaran/jadwal_pelajaran&karyawan_name="+karyawan_name+"&karyawan_id="+karyawan_id+"&department_id="+department_id+"&tahun_ajaran_id="+tahun_ajaran_id);
		});
		$("#tahun_ajaran_id").change(function(){
			var karyawan_id=$("#karyawan_id").val();
			var	department_id=$("#department_id").val();
			var	karyawan_name=$("#karyawan_name").val();				
			var	tahun_ajaran_id=$("#tahun_ajaran_id").val();				
			window.location.replace("index.php?r=akademik/guru_pelajaran/jadwal_pelajaran&karyawan_name="+karyawan_name+"&karyawan_id="+karyawan_id+"&department_id="+department_id+"&tahun_ajaran_id="+tahun_ajaran_id);
		});	
							
	});	
	

</script>
<script>
	function popupResult(obj){
		console.log(obj);
		$("#karyawan_name").val(obj.name);
		$("#karyawan_id").val(obj.id);
		var karyawan_id =$("#karyawan_id").val();
		var	department_id=$("#department_id").val();
		var	karyawan_name=$("#karyawan_name").val();		
		var	tahun_ajaran_id=$("#tahun_ajaran_id").val();				
		window.location.replace("index.php?r=akademik/guru_pelajaran/jadwal_pelajaran&karyawan_name="+karyawan_name+"&karyawan_id="+karyawan_id+"&department_id="+department_id+"&tahun_ajaran_id="+tahun_ajaran_id);
	}
</script>		
<div>
<form method="post" id="frmFilter" name="frmFilter">
    <?php
		//$department_id = Yii::app()->request->getParam("department_id","KB");
		$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","1");
		$karyawan_id = Yii::app()->request->getParam("karyawan_id","0");				
		$karyawan_name = Yii::app()->request->getParam("karyawan_name","");						
        $rsDep = MDepartment::model()->findAll(array("condition"=>"is_active = TRUE","order"=>"id"));
        $arrOptions=array();
        foreach($rsDep as $rw){
            $selected = $department_id == $rw->id ? true : false;
            $arrOptions[] = array("label"=>$rw->name,"value"=>$rw->id,"selected"=>$selected);
        }
        $this->widget('application.components.widgets.SelectField', 
            array("name"=>"department_id",
                "value"=>$department_id,
                "htmlOptions"=>"",
                "label"=>array(
                    "value"=>"Department",
                    "htmlOptions"=>"class ='form mandatory' style='display:inline-block;width:100px'"
                ),
                "options"=>$arrOptions,
            )
        );
        
    ?>
    </form>		
</div>
<div>
    <?php 
		echo "<label class='form mandatory' style='width:100px;'>Pengajar</label>";
		$this->widget('application.components.widgets.FindTextField', 
			array('options' => array('id'=>'karyawan_name','value'=>$karyawan_name,),
				'windowParams' => array('url'=>"index.php?r=data_popup/Pengajar&callback=popupResult",'name' => 'popup_target',  
					'specs'=> array('height'=>500),
				),
				'callback'=>"popupResult",
				'formId'=>"frmFilter",
			)
		);			
    ?>		
    <input type="hidden" id="karyawan_id" name="karyawan_id" value="<?php echo $karyawan_id ?>"  />
    <input type="hidden" id="karyawan_name" name="karyawan_name" value="<?php echo $karyawan_name ?>"  />

</div>    

<div>
    <?php
        //$dep = Yii::app()->request->getParam("department","0");
        $rsDep = MTahunAjaran::model()->findAll(array("condition"=>"is_active = TRUE and department_id='$department_id'","order"=>"id"));
        $arrOptions=array();
        foreach($rsDep as $rw){
            $selected = $tahun_ajaran_id == $rw->id ? true : false;
            $arrOptions[] = array("label"=>$rw->name,"value"=>$rw->id,"selected"=>$selected);
        }
        $this->widget('application.components.widgets.SelectField', 
            array("name"=>"tahun_ajaran_id",
                "value"=>$tahun_ajaran_id,
                "htmlOptions"=>"",
                "label"=>array(
                    "value"=>"Tahun Ajaran",
                    "htmlOptions"=>"class ='mandatory' style='display:inline-block;width:100px'"
                ),
                "options"=>$arrOptions,
            )


        );
        
    ?>		
</div>
  
<div style="float:rigth">&nbsp;
<?php 
    $this->widget('application.components.widgets.ActionBar', 
        array("menuAlign" => "right", 
            'items' => array(
                array(
                    'id' => "btnRefresh",
                    'title' => Yii::t("menu","Segarkan"),
                    'icon' => "images/action/Action-Refresh-icon.png",
                    'callback'=>"reloadPage"
                ),					
              /*  array(
                    'id' => "btnNew",
                    'title' => Yii::t("menu","Baru"),
                    'icon' => "images/action/Actions-document-new-icon.png",
                    'callback'=>"newData"
                ), */
                array(
                    'id' => "btnNew",
                    'title' => Yii::t("menu","Cetak"),
                    'icon' => "images/action/Actions-document-print-icon2.png",
                    'callback'=>"printData"
                ),					
                 
            ) 
        )
    );
?>
</div>	
    <!--
	<div style="clear:both"></div>	
	<table id="tblData"><tr></tr></table>
	<div id="navTblData"></div>
    --> 
<style>
table.jadwal, td, tr
{
border:1px solid grey;  
text-align:center; 
}
tr
{

}

</style>

<div>
	<?php  
		$x=0;
		if ($karyawan_id>0)
		{
			echo '<table  class="jadwal" ><tr><td  width=150>Jam</td><td width=120>Senin</td><td width=120>Selasa</td><td width=120>Rabu</td><td width=120>Kamis</td><td width=120>Jumat</td><td width=120>Sabtu</td><td width=120>Minggu</td></tr>';
			$rsDep = MJamPelajaran::model()->findAll(array("condition"=>"department_id = '".$department_id."' " ,"order"=>"id"));
			$json = CJSON::encode($rsDep); 
			$arrjam = json_decode($json,true);
			$dataarray=array();
			$dataarrayd=array();
			$dataarrays=array();			
			$dataarrayacc=array();
			$dataarraydetail=array();
			$dataarraypelajaran_id=array();
			$dataarraystatus=array();
		
			for ($xhari=1; $xhari<=7; $xhari++)
			{
				$mcon= Yii::app()->db;
				$sql1='select A.id,department_id,tahun_ajaran_id,kelas_id,karyawan_id,pelajaran_id,status,hari,pelajaran_ke,B.is_active from m_jam_pelajaran A left join m_jadwal_pelajaran B on A.id=B.pelajaran_ke  left join m_kelas_tahun_ajaran F on B.kelas_tahun_ajaran_id=F.id where   (isnull(B.hari) or B.hari='.$xhari.') and karyawan_id='.$karyawan_id .' and department_id="'.$department_id.'" and tahun_ajaran_id='.$tahun_ajaran_id;
				$mJadwalPelajaran=$mcon->createCommand($sql1);		
				$rsDep=$mJadwalPelajaran->queryAll();	
				$json = CJSON::encode($rsDep);
				$arrjamcek = json_decode($json,true);
			}
			
			for ($xhari=1; $xhari<=7; $xhari++)
			{	
				$y=0;
				for ($xjam=0; $xjam<count($arrjam); $xjam++)
				{
				$dataarrayacc[$xhari][$xjam]=0;
				//echo $dataarrayacc[$xhari][$xjam] .'-h:'.$xhari.'jam:'.$xjam.'<br>';
					$count=0;
					$jamid = $arrjam[$xjam]['id'];
					$mcon= Yii::app()->db;
					$sql1='select A.id as id,F.tahun_ajaran_id,F.kelas_id,A.karyawan_id,A.pelajaran_id,A.status,A.hari,A.pelajaran_ke,A.pelajaran_sampai,A.is_active,A.keterangan, C.name as kelas_name, D.name as pelajaran_name from m_jadwal_pelajaran A  left join m_kelas_tahun_ajaran F on A.kelas_tahun_ajaran_id=F.id left join m_kelas C on F.kelas_id=C.id left join m_pelajaran D on A.pelajaran_id=D.id  where  ('.$jamid.' >= pelajaran_ke and '.$jamid.' <= pelajaran_sampai ) and hari='.$xhari.' and karyawan_id='.$karyawan_id.' and department_id="'.$department_id.'"  and tahun_ajaran_id='.$tahun_ajaran_id	;
					$mJadwalPelajaran=$mcon->createCommand($sql1);		
					$rsDep=$mJadwalPelajaran->queryAll();
				
					$json = CJSON::encode($rsDep);
					$arrrow = json_decode($json,true);
					$count = $count + count($arrrow);
					//echo $count.'<br>';
					$dataarrays[$xhari][$xjam]=0;
					if ($count>0){$x=$x+$count	;} else {$x=$count;}
//					echo $x;
					$dataarrayd[$xhari][$xjam] =$x;
					for ($xj=0; $xj<count($arrrow); $xj++)
					{
						switch ($arrrow[$xj]['status']) 
						{
							case 1:
								$data_status_name = 'Mengajar';
								break;
							case 2:
								$data_status_name = 'Asistensi';
								break;
							case 3:
								$data_status_name = 'Tambahan';
								break;
						}					
						$dataarrayacc[$xhari][$xjam] =$arrrow[$xj]['id'];
		//				echo $dataarrayacc[$xhari][$xjam] .'-h:'.$xhari.'jam:'.$xjam.'<br>';
						$dataarraydetail[$xhari][$xjam] =$arrrow[$xj]['kelas_name'].'<br>'.$data_status_name."<br>".$arrrow[$xj]['pelajaran_name'];
						$dataarraypelajaran_id[$xhari][$xjam] =$arrrow[$xj]['pelajaran_id'];	
						$dataarraystatus[$xhari][$xjam] =$arrrow[$xj]['status'];												
						$dataarrays[$xhari][$xjam]= $arrrow[$xj]['pelajaran_sampai']-$arrrow[$xj]['pelajaran_ke']+1;
						$next=$dataarrayacc[$xhari][$xjam];
						if ($xjam>0){
						$prev=$dataarrayacc[$xhari][$xjam-1];						
						}
						else {$prev=123456;}
						if ($next!=$prev){$y=0;}						
						if ($count>0){$y=$y+$count;} else {$y=$count;}
						$dataarrayd[$xhari][$xjam] =$y;			
					}			
				}	

				for ($xjam=0; $xjam<count($arrjam); $xjam++)
				{
					{
					$dataarray[$xhari][$xjam] = $dataarrayd[$xhari][$xjam];		
					$dataarrayd[$xhari][$xjam]=$dataarrays[$xhari][$xjam];
					} 
				}	
			}
		
			for ($xjam=0; $xjam<count($arrjam); $xjam++)
			{
				echo '<tr><td height=60>'.$arrjam[$xjam]['name'].'  </td>';
				for ($xhari=1; $xhari<=7; $xhari++)		
				{
					$n=0;
					for ($xjams=0; $xjams<count($arrjam); $xjams++)
					{
						if (($dataarray[$xhari][$xjam]>0) and ($dataarray[$xhari][$xjams]>0))
						{
							if ($xjams>=$xjam)
							{
								$xjamp=$xjams+1	;
								//$xjamm=$xjams-1	;
								if (count($arrjam)>1){
								if ($dataarray[$xhari][$xjamp]>$dataarray[$xhari][$xjams])
								{
									($n=$n+1);
								} 
								else 
								{
									$n=$dataarray[$xhari][$xjams];
								}}
								else
								{$n=$dataarray[$xhari][$xjams];}
							//echo $n;
							}
						}
					}
					 
					if ($n>0)
					{
						$n=$dataarrayd[$xhari][$xjam];
						if ($n==1)
						{ 
							echo "<td align=center>".$dataarraydetail[$xhari][$xjam]. "<br><a href='#' onclick='editRow(".$dataarrayacc[$xhari][$xjam].",".$arrjam[$xjam]['id'].",".$arrjam[$xjam]['id'].",".$xhari.",".$dataarraypelajaran_id[$xhari][$xjam].",".$dataarraystatus[$xhari][$xjam].");'><img src='images/action/Actions-Edit-icon.png' border=0 width=20></a>  <a href='#' onclick='deleteRow(".$dataarrayacc[$xhari][$xjam].");'><img src='images/action/Actions-dialog-cancel-icon.png' border=0 width=20></a> </td>";
						} 
						else 
						{
							//echo '<td>'.$dataarray[$xhari][$xjam].'data'.$n.'**'.$dataarrayd[$xhari][$xjam].'</td>';
							if ($dataarray[$xhari][$xjam]==1)
							{
								$next_jam=$arrjam[$xjam]['id']+$n-1;
								//print_r($arrjam);
								echo "<td rowspan=".$n." align=center>".$dataarraydetail[$xhari][$xjam]."<br><a href='#' onclick='editRow(".$dataarrayacc[$xhari][$xjam].",".$arrjam[$xjam]['id'].",".$next_jam.",".$xhari.",".$dataarraypelajaran_id[$xhari][$xjam].",".$dataarraystatus[$xhari][$xjam].");'><img src='images/action/Actions-Edit-icon.png' border=0 width=20></a></a> <a href='#' onclick='deleteRow(".$dataarrayacc[$xhari][$xjam].");'><img src='images/action/Actions-dialog-cancel-icon.png' border=0 width=20></a> </td>";
								//echo "<td  rowspan=".$n."> ".$arrjam[$xjam]['id']."- hari -".$xhari."r</a> </td>";
							}
							else 
							{
								echo '';
							}
						}
					}
					else 
					{ 
						echo "<td align=center width=120><a href='#' onclick='newData(".$arrjam[$xjam]['id'].",".$xhari.");'><img src='images/action/Actions-list-add-icon.png' border=0 width=20></a> </td>";
						//echo "<td align=center> <a href='#' onclick='newData(".$arrjam[$xjam]['id'].",".$xhari.");'>+".$arrjam[$xjam]['id']." hari ".$xhari."r</a> </td>";
					}
				}
			}	
		}
		echo '</table>';	
    ?>
</div>